<?php

namespace app\common\model;

use think\Model;

/**
 * 公众号
 */
class Wechats Extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    // 追加属性
    protected $append = [
    ];
    // 获取列表
    public function getItems($field="id,wxname",$codition=[]){
    	return $this->where($codition)->field($field)->column('wxname','id');;
    }

    
   
    /*从微信端获取到Token_Access*/
    public function Token_Access($option=null,$token=null){
        if(empty($option)){
            $option = self::getByToken($token);
        }
        if(isset($option['access_token']) && !empty($option['access_token']) && ($option['dateline'] > (time()-7000))){
            return $option['access_token'];
        }
        $weobj = WX($option);
        //$url_get='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsec;
        $access_token = $weobj->checkAuth();//\Org\Util\Curl::_get($url_get);
        if($access_token){
            self::where(['id'=>$option['id']])->update(['access_token'=>$access_token,'dateline'=>time()]);
            return $access_token;
        }
        return false;
    }
}
